package org.middleware.service.system;

import org.middleware.common.BusinessException;
import org.middleware.dao.system.SysDepartmentsDao;
import org.middleware.dao.system.SysUsersDao;
import org.middleware.entity.system.SysDepartments;
import org.middleware.entity.system.SysMenus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Stack;

/**
* @author zhiqiangwu
* @description 针对表【sys_departments(部门表)】的数据库操作Service
* @createDate 2025-09-05 16:40:01
*/
@Service
public class SysDepartmentsService {

    @Autowired
    private SysDepartmentsDao departmentsDao;

    @Autowired
    private SysUsersDao usersDao;

    public List<SysDepartments> getDepartmentByEnterpriseId(int id, int isAdmin) {
        return departmentsDao.getDepartmentByEnterpriseId(id);
    }

    public int create(SysDepartments department) {
        if (department.getEnterpriseId() == null || department.getEnterpriseId() < 0) {
            throw new BusinessException("企业不能为空");
        }
        if (department.getName() == null || department.getName().trim().length() == 0) {
            throw new BusinessException("部门名称不能为空");
        }
        return departmentsDao.create(department);
    }

    public int update(SysDepartments department) {
        if (department.getId() == null || department.getId() < 0) {
            throw new BusinessException("部门信息异常");
        }
        if (department.getEnterpriseId() == null || department.getEnterpriseId() < 0) {
            throw new BusinessException("企业不能为空");
        }
        if (department.getName() == null || department.getName().trim().length() == 0) {
            throw new BusinessException("部门名称不能为空");
        }
        return departmentsDao.update(department);
    }

    /**
     * 删除指定菜单
     * @param id 删除指定id的菜单
     * @return
     */
    public int delete(int id) {
        if (id < 0) {
            throw new BusinessException("部门信息不存在");
        }
        return departmentsDao.delete(id);
    }

    /**
     * 删除部门及其下级部门
     * @param id 部门id
     * @return
     */
    public int deleteWithDownDepartment(int id) {
        if (id < 0) {
            throw new BusinessException("请输入正确部门");
        }
        List<SysDepartments> allDepartments = departmentsDao.getDepartmentByEnterpriseId(0);
        if (allDepartments == null) {
            throw new BusinessException("要删除的菜单不存在");
        }

        List<Integer> children = handleDeleteDownDepartment(allDepartments, id);
        // 检查部门下有没有人
        int users = usersDao.countUsersByDepartmentList(children);
        if (users > 0) {
            throw new BusinessException("部门下用户不为空，请先修改用户的部门");
        }

        return departmentsDao.deleteDepartmentWithChildren(children);

    }

    private List<Integer> handleDeleteDownDepartment(List<SysDepartments> departments, int id) {
        List<Integer> result =  new ArrayList<>();
        result.add(id);
        for (SysDepartments department : departments) {
            if (department.getParentId() == id) {
                result.addAll(handleDeleteDownDepartment(departments, department.getId()));
            }
        }
        return result;
    }

    /**
     * 查询企业id对应的部门树数据
     * @param enterprise 企业id
     * @return
     */
    public List<SysDepartments> getDepartmentTree(int enterprise) {
        if (enterprise <= 0) {
            throw new BusinessException("企业信息不能为空");
        }
        List<SysDepartments> departments = departmentsDao.getDepartmentByEnterpriseId(enterprise);
        return buildDepartmentTree(departments, 0);
    }

    /**
     * 构建部门树
     * @param departmentList 部门列表
     * @param parent_id
     * @return
     */
    private List<SysDepartments> buildDepartmentTree(List<SysDepartments> departmentList, int parent_id) {
        List<SysDepartments> tree = new ArrayList<>();
        for (SysDepartments department : departmentList) {
            if (parent_id == department.getParentId()) {
                List<SysDepartments> children = buildDepartmentTree(departmentList, department.getId());
                department.setChildren(children);
                tree.add(department);
            }
        }
        // 按排序字段排序
        tree.sort(Comparator.comparingInt(SysDepartments::getSort));
        return tree;
    }

}
